/**
 * Created by puban on 2017/3/1.
 */

import axios from 'axios'
import qs from 'qs'

import  router from '../router/index'
axios.defaults.timeout = 5000;

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

if (process.env.NODE_ENV === 'production') {

  axios.defaults.baseURL = '/api/douban';

} else {
  axios.defaults.baseURL = 'http://192.168.1.189:3000/api/douban';
}

// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;

//POST传参序列化
axios.interceptors.request.use((config) => {
  if (config.method === 'post') {
    config.data = qs.stringify(config.data);
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});

//code状态码200判断
axios.interceptors.response.use((res) => {
  // if (res.data.code != '200') {
  //   return Promise.reject(res);
  // }
  return res;
}, (error) => {

  return Promise.reject(error);
});


export default {
  get(url = '', params = {}) {
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params: params
      }).then(res => {
        resolve(res)
      }).catch(e => {
        console.log(e)
        alert('网络异常！请重新刷新页面')
        reject(e)
      })
    })
  },
  post(url = '', params = {}){
    return new Promise((resolve, reject) => {
      axios.get(url, params).then(res => {
        resolve(res)
      }).catch(e => {
        console.log(e)
        alert('网络异常！请重新刷新页面')
        reject(e)
      })
    })
  }
}



